﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace HelloXNA.Component.Sprite
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class SpriteSortModelDemo : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch _spriteBatch;

        Texture2D _spriteSon;
        Texture2D _spriteMVP;

        public SpriteSortModelDemo(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            _spriteSon = Game.Content.Load<Texture2D>("Image/Son");
            _spriteMVP = Game.Content.Load<Texture2D>("Image/MVP");
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Game.GraphicsDevice.Clear(Color.CornflowerBlue);

            _spriteBatch.Begin(SpriteSortMode.Deferred, null);
            _spriteBatch.Draw(_spriteSon, Vector2.Zero, Color.White);
            _spriteBatch.Draw(_spriteSon, new Vector2(20, 0), Color.Red);
            _spriteBatch.End();

            _spriteBatch.Begin(SpriteSortMode.Immediate, null);
            _spriteBatch.Draw(_spriteSon, new Vector2(400, 0), Color.White);
            _spriteBatch.Draw(_spriteSon, new Vector2(420, 0), Color.Red);
            _spriteBatch.End();

            _spriteBatch.Begin(SpriteSortMode.FrontToBack, null);
            Draw(_spriteSon, new Vector2(20, 200), Color.Red, 0.2f);
            Draw(_spriteSon, new Vector2(0, 200), Color.White, 0.1f);
            _spriteBatch.End();

            _spriteBatch.Begin(SpriteSortMode.BackToFront, null);
            Draw(_spriteSon, new Vector2(420, 200), Color.Red, 0.2f);
            Draw(_spriteSon, new Vector2(400, 200), Color.White, 0.1f);
            _spriteBatch.End();

            _spriteBatch.Begin(SpriteSortMode.Texture, null);
            _spriteBatch.Draw(_spriteSon, new Vector2(20, 400), Color.White); //2k (If the texture is small, draw it is better.)
            _spriteBatch.Draw(_spriteMVP, new Vector2(0, 400), Color.White); // 10k
            _spriteBatch.End();


            base.Draw(gameTime);
        }


        private void Draw(Texture2D texture, Vector2 postion, Color color, float layerDepth)
        {
            _spriteBatch.Draw(texture, postion, null, color, 0f, Vector2.Zero, 1f, SpriteEffects.None, layerDepth);
        }
    }
}
